import { Component, OnInit } from '@angular/core'
import { AcquirerNewBaseClass } from '../baseClass'
import { FormBuilder } from '@angular/forms'
import { AcquirerStoreService } from '../acquirer-store.service'
import { AuditConfig, EAuditConfigType } from '@shared/interfaces/acquirer'
import { I18NService } from '@core'

@Component({
  selector: 'app-acquirer-new-audit',
  templateUrl: './audit.component.html',
})
export class AcquirerNewAuditComponent extends AcquirerNewBaseClass implements OnInit {
  /** 审批层数 */
  levelNum = 9

  /** 是否需要审核 */
  isAudit = false

  /** 审核层级选项 */
  auditOptions = []

  /** 审核设置 */
  auditConfigs: AuditConfig[]

  /** 审核类型 */
  auditConfigTypeEnum = EAuditConfigType

  /** 门店录入审核层级 */
  storeAudit = '1'

  /** 是否开启门店录入 */
  isStoreAudit = false

  /** 机构退款审核层级 */
  transRefundAudit = '1'

  /** 是否开启机构退款 */
  isTransRefund = false

  /** 门店汇率审核层级 */
  exchangeRateAudit = '1'

  /** 是否开启门店汇率 */
  isExchangeRate = false

  /** 风控审核层级 */
  riskControlAudit = '1'

  /** 是否开启风控 */
  isRiskControl = false

  /** 是否开启运营功能 */
  isOperation = false

  /** 运营功能层级 */
  operationAudit = '1'

  constructor(
    private i18n: I18NService,
    public fb: FormBuilder,
    public acquirerStore: AcquirerStoreService
  ) {
    super(fb, acquirerStore)
  }

  ngOnInit() {
    this.initAuditOptions()
    this.initFormValue()
  }

  initAuditOptions(): void {
    for (let i = 0; i < this.levelNum; i++) {
      const option = {
        label: i + 1,
        value: i + 1 + '',
      }
      this.auditOptions.push(option)
    }
  }

  initFormValue(): void {
    const { auditConfigs } = this.acquirer.institution.common
    this.auditConfigs = auditConfigs
    if (auditConfigs.length > 0) {
      this.isAudit = true
    }
    this.readDataFromAuditConfig()
  }

  handleIsAuditChange(isAudit: boolean): void {
    if (!isAudit) {
      // 清空
      this.auditConfigs = []
    }
  }

  checkValid(): boolean {
    this.pushDataToStore()
    return true
  }

  pushDataToStore(): void {
    if (this.isAudit) {
      this.writeConfigToAuditConfig()
    }
    this.acquirer.institution.common.auditConfigs = this.auditConfigs
  }

  private readDataFromAuditConfig(): void {
    this.auditConfigs.forEach(config => {
      const { type } = config
      switch (type) {
        case this.auditConfigTypeEnum.Store:
          this.storeAudit = config.stepNumber + ''
          this.isStoreAudit = true
          break

        case this.auditConfigTypeEnum.TransRefund:
          this.transRefundAudit = config.stepNumber + ''
          this.isTransRefund = true
          break

        case this.auditConfigTypeEnum.ExchangeRate:
          this.exchangeRateAudit = config.stepNumber + ''
          this.isExchangeRate = true
          break

        case this.auditConfigTypeEnum.RiskControl:
          this.riskControlAudit = config.stepNumber + ''
          this.isRiskControl = true
          break

        case this.auditConfigTypeEnum.Operation:
          this.operationAudit = config.stepNumber + ''
          this.isOperation = true
          break

        default:
          console.error('read data from audit config error')
      }
    })
  }

  private writeConfigToAuditConfig(): void {
    if (this.isStoreAudit) {
      this.auditConfigs.push({
        type: this.auditConfigTypeEnum.Store,
        stepNumber: parseInt(this.storeAudit, 10),
      })
    }

    if (this.isExchangeRate) {
      this.auditConfigs.push({
        type: this.auditConfigTypeEnum.ExchangeRate,
        stepNumber: parseInt(this.exchangeRateAudit, 10),
      })
    }

    if (this.isRiskControl) {
      this.auditConfigs.push({
        type: this.auditConfigTypeEnum.RiskControl,
        stepNumber: parseInt(this.riskControlAudit, 10),
      })
    }

    if (this.isTransRefund) {
      this.auditConfigs.push({
        type: this.auditConfigTypeEnum.TransRefund,
        stepNumber: parseInt(this.transRefundAudit, 10),
      })
    }

    if (this.isOperation) {
      this.auditConfigs.push({
        type: this.auditConfigTypeEnum.Operation,
        stepNumber: parseInt(this.operationAudit, 10),
      })
    }
  }

  trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }
}
